﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HidesaburoDotNET.Core;

namespace HidesaburouDotNetTest.Test.YakuCheck
{
    public class MockAgariTehai : AgariTehai
    {

        public static Mentsu CreateMentsu(MentsuType type, Hai headHai)
        {
            switch(type & MentsuType.Type){
                case MentsuType.Ankan:
                    return new Mentsu(type,
                        new List<Hai>(){headHai,new Hai(headHai.HaiIndex),new Hai(headHai.HaiIndex),new Hai(headHai.HaiIndex)});
                case MentsuType.Anko:
                    return new Mentsu(type,
                        new List<Hai>(){headHai, new Hai(headHai.HaiIndex), new Hai(headHai.HaiIndex)});
                case MentsuType.Syuntsu_A:
                    return new Mentsu(type,
                        new List<Hai>(){headHai, new Hai(headHai.HaiIndex + 1), new Hai(headHai.HaiIndex + 2)});
                case MentsuType.Toitsu:
                    return new Mentsu(type,
                        new List<Hai>(){headHai, new Hai(headHai.HaiIndex)});
            }
            return null;
        }


        Player player = new Player();
        List<Hai> hai = new List<Hai>();
        List<Mentsu> mentsu = new List<Mentsu>();
        bool menzen = false;
        bool tsumo = false;
        Hai agariHai;
        Mentsu agariMentsu;

        public MockAgariTehai(List<Mentsu> mentsu)
        {
            this.mentsu = mentsu;

            hai = new List<Hai>(ListUpHaiFromMentsu(mentsu));

            menzen = !mentsu.Exists(m => (m.Type & MentsuType.Naki) != MentsuType.None);

        }


        private IEnumerable<Hai> ListUpHaiFromMentsu(List<Mentsu> mentsu)
        {
            foreach (Mentsu m in mentsu)
            {
                foreach (Hai h in m.Hais)
                {
                    yield return h;
                }
            }
        }

        public void SelectAgariHai(int mentsuIndex,int haiIndex)
        {
            agariMentsu = mentsu[mentsuIndex];
            agariHai = agariMentsu[haiIndex];
            /*if (!tsumo)
            {
                if (agariMentsu.Type == MentsuType.Syuntsu_A)
                {
                    agariMentsu.Type = MentsuType.Syuntsu_M;
                }
                else if (agariMentsu.Type == MentsuType.Anko) 
                {
                    agariMentsu.Type = MentsuType.Minko;
                }
            }*/
        }



        #region AgariTehai メンバ

        public Player Player
        {
            get { return player; }
        }

        public List<Mentsu> Mentsu
        {
            get { return mentsu; }
        }

        public List<Hai> Hai
        {
            get { return hai; }
        }

        public Hai AgariHai
        {
            get { return agariHai; }
        }

        public Mentsu AgariMentsu
        {
            get { return agariMentsu; }
        }

        public bool Tsumo
        {
            get { return tsumo; }
            set { 
                this.tsumo = value;
                if ( !tsumo && agariMentsu != null)
                {

                    if (agariMentsu.Type == MentsuType.Syuntsu_A)
                    {
                        agariMentsu.Type = MentsuType.Syuntsu_M;
                    }
                    else if (agariMentsu.Type == MentsuType.Anko)
                    {
                        agariMentsu.Type = MentsuType.Minko;
                    }
                }
            }
        }

        public bool Menzen
        {
            get { return menzen; }
        }

        #endregion
    }
}
